window.onload  = initialize;

var clsTagName = "className";
if (window.ActiveXObject)
     clsTagName = "className"
else if (document.getBoxObjectFor)
     clsTagName = "class"

function initialize() {
	var tables = $('table');
	tables.each(function() {

		var clsName = $(this).attr(clsTagName);
		//表格样式自动填充
		if(clsName.indexOf("activeTable")>=0) {

			var trs = $(this).find('tr');
			var j=0;
			var phoneIndex;
			//第一行是标题，不控制
			trs.each(function() {
			if(j++>0) {
				var children = $(this).find('td');
				var l = 0;
				children.each(function() {
					l++;
					if(j%2==0) {
						$(this).attr("prevCls", "tablecolour2");
						$(this).attr(clsTagName, "tablecolour2");
					} else {
						$(this).attr("prevCls", "tablecolour2");
						$(this).attr(clsTagName, "tablecolour2");
					}
					var tdContent = $(this).html();

					var s = 12;
					if(l==phoneIndex) {
						s = 6;
					}
					if(tdContent.indexOf('<')<0) {
						if(Len($(this).text())>s) {
							$(this).attr("title", $(this).text());
							$(this).text(substring($(this).text(), 0, s)+"..");
						}
					}
					
				})
				
				$(this).css('cursor', "pointer");
				
				$(this).mouseover(function() {
					var children2 = $(this).find('td');
					children2.each(function() {
						$(this).attr(clsTagName, "tablecolour1");
					});
				})
					
				$(this).mouseout(function() {
					var children2 = $(this).find('td');
					children2.each(function() {
						$(this).attr(clsTagName, $(this).attr('prevCls'));
					})
				})
				}
				else {
					var i = 0;
					var children = $(this).find('td');
	
					children.each(function() {
						i++;

						if($(this).text().indexOf("phone")>=0) {
							phoneIndex = i;
						}
					})
				}
				
			})
		}
	})

}

function Len(str) {
     var i,sum;
     sum=0;
     for(i=0;i<str.length;i++) {
         if ((str.charCodeAt(i)>=0) && (str.charCodeAt(i)<=255))
             sum=sum+1;
         else
             sum=sum+2;
     }
     return sum;
}

function substring(str, from, to) {
	 var i,j=0;
	 var rs = "";
  
     for(i=from;i<to;i++) {
     	var s = str.charCodeAt(i);
        if ((s>=0) && (s<=255)) {
             rs+=str.charAt(j++);
        }
        else {
             rs+=str.charAt(j++);
             i++;
        }
     }
     return rs;
}